import { defineStore } from 'pinia'
import axios from 'axios'

export const useBlogStore = defineStore('blog', {
  state: () => ({
    blogs: [],
    loading: false,
    error: null
  }),

  actions: {
    // 获取所有博客
    async getAllBlogs(params) {
      this.loading = true
      try {
        const response = await axios.get('/api/blog/list', { params })
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '获取博客列表失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    // 更新博客
    async updateBlog(blogId, blogData) {
      this.loading = true
      try {
        const response = await axios.put(`/api/blog/update/${blogId}`, blogData)
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '更新博客失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    // 更新博客发布状态
    async updatePublishStatus(blogId, isPublished) {
      this.loading = true
      try {
        const response = await axios.put(`/api/blog/publish/${blogId}`, null, {
          params: { isPublished }
        })
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '更新博客状态失败'
        throw error
      } finally {
        this.loading = false
      }
    },

    // 删除博客
    async deleteBlog(blogId) {
      this.loading = true
      try {
        const response = await axios.delete(`/api/blog/${blogId}`)
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || '删除博客失败'
        throw error
      } finally {
        this.loading = false
      }
    }
  }
}) 